package com.yiyuwanglu.basecore.web.controller;

import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mongodb.DBObject;
import com.mongodb.util.JSON;
import com.yiyuwanglu.basecore.page.Page;
import com.yiyuwanglu.basecore.service.INStatisticsService;
import com.yiyuwanglu.basecore.service.IStatisticsService;
import com.yiyuwanglu.basecore.util.StatisticsUtils;
import com.yiyuwanglu.basecore.vo.CountGroupVo;
import com.yiyuwanglu.basecore.vo.QueryMongoVo;
import com.yiyuwanglu.basecore.vo.UserActionDetailVo;
import com.yiyuwanglu.basecore.web.common.ResultMap;
import com.yiyuwanglu.basecore.web.common.ResultMap.STATUS;

@Controller("statisiticsController")
@RequestMapping("/statisitics")
public class StatisticsController {

	@Resource(name = "statisiticsService")
	private IStatisticsService statisticsService;

	@Autowired
	INStatisticsService nStatisticsService;

	private String keyCode = "100000,101000,102000,103000,104000,105000,106000";

	@RequestMapping("/saveAllData")
	@ResponseBody
	public ResultMap saveAllData(HttpServletRequest request, HttpServletResponse response) {
		ResultMap resultMap = new ResultMap();

		try {
			List<String> list = new ArrayList<>();
			
			list.add("{'type': '110202','host': 'm.superfa.cn','param': '39f96947-b792-11e6-ba00-d89d672b90bc','path': '/product/shopxtzgdetail/39f96947-b792-11e6-ba00-d89d672b90bc','shopId': '13655521397','source': 'WAP', 'wechatId': 'oFrIbtxYljnfX8BAq3PkWOUgQvkE', 'productName': 'null','createDate': '2016-11-08 12:11:35','ip': '61.190.128.214','system': 'admin'}");
			list.add("{'type': '110202','host': 'm.superfa.cn','param': '2c190640-ccc8-11e6-b684-ecf4bbce3884','path': '/product/shopxtzgdetail/2c190640-ccc8-11e6-b684-ecf4bbce3884','shopId': '13655521397','source': 'WAP', 'wechatId': 'oFrIbt_07VY99k6UE9zfFndYxwPk', 'productName': 'null','createDate': '2016-11-08 12:14:53','ip': '112.123.220.59','system': 'admin'}");
			
			DBObject o = null;
			for(String data : list){
				o = (DBObject) JSON.parse(data);
				statisticsService.wapDataInsert(o);
			}

			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.setStatus(STATUS.ERROR);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SYSTEM_ERROE);
		}

		return resultMap;
	}
	
	@RequestMapping("/saveBaseData")
	@ResponseBody
	public ResultMap saveBaseData(HttpServletRequest request, HttpServletResponse response, String data, String ip) {
		ResultMap resultMap = new ResultMap();

		try {
			DBObject o = (DBObject) JSON.parse(data);
			Object productName = o.get("productName");
			if (!StringUtils.isBlank(productName + "")) {
				o.put("productName", URLDecoder.decode(productName + "", "utf-8"));
			}
			o.put("createDate", StatisticsUtils.dateFormat("yyyy-MM-dd HH:mm:ss"));
			o.put("ip", ip);
			
			Object source = o.get("source");
			if(!StringUtils.isBlank(source + "") && ("WAP").equals(source)){
				statisticsService.wapDataInsert(o);
			}else{
				statisticsService.baseDataInsert(o);
			}

			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			resultMap.setAttribute("data", data);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.setStatus(STATUS.ERROR);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SYSTEM_ERROE);
		}

		return resultMap;
	}

	@RequestMapping("/queryDateList")
	@ResponseBody
	public ResultMap queryDateList(HttpServletRequest request, HttpServletResponse response, QueryMongoVo data,
			Page pager) {
		ResultMap resultMap = new ResultMap();
		try {
			if (null != data.getEndDate() && !"".equals(data.getEndDate().trim())) {
				data.setEndDate(StatisticsUtils.getNextDay(data.getEndDate()));
			}
			List<CountGroupVo> bean = nStatisticsService.getStatisticsDetail(CountGroupVo.class, data, "statistics", pager);
			resultMap.setAttribute("list", bean);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			resultMap.setAttribute("pager", pager);
			// 服务器时间
			resultMap.setAttribute("nowDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		} catch (Exception e) {
			resultMap.setStatus(STATUS.ERROR);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SYSTEM_ERROE);
			resultMap.setMessage(e.getMessage());
		}
		return resultMap;
	}

	@RequestMapping("/queryDateDetail")
	@ResponseBody
	public ResultMap queryDateDetail(HttpServletRequest request, HttpServletResponse response, QueryMongoVo data) {
		ResultMap resultMap = new ResultMap();
		try {
			if (null != data.getEndDate() && !"".equals(data.getEndDate().trim())) {
				data.setEndDate(StatisticsUtils.getNextDay(data.getEndDate()));
			}
			// List<CountGroupVo> info =
			// nStatisticsService.getInfo(CountGroupVo.class, data,
			// "statistics");
			List<CountGroupVo> info = nStatisticsService.getStatisticsDetail(CountGroupVo.class, data, "statistics");

			// 分类
			UserActionDetailVo insideActionDetailVo = new UserActionDetailVo();
			insideActionDetailVo.setActionList(new ArrayList<CountGroupVo>());
			List<UserActionDetailVo> actionList = new ArrayList<UserActionDetailVo>();
			// 分类LIST
			for (CountGroupVo countGroupVo : info) {
				if (keyCode.indexOf(countGroupVo.getType()) >= 0) { // 判断是否是关键词
					if (null != insideActionDetailVo.getActionList()
							&& insideActionDetailVo.getActionList().size() > 0) {// 出现关键字且不是第一关键字，已存在的入结果集
						actionList.add(insideActionDetailVo);
					}
					insideActionDetailVo = new UserActionDetailVo();// //
																	// 出现关键字，清空临时对象
					insideActionDetailVo.setActionList(new ArrayList<CountGroupVo>());
					insideActionDetailVo.setKey(countGroupVo.getType());
				}
				insideActionDetailVo.getActionList().add(countGroupVo);
			}
			// 最后一个记录添加到结果集
			actionList.add(insideActionDetailVo);
			resultMap.setAttribute("info", actionList);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			resultMap.setStatus(STATUS.ERROR);
			resultMap.setStatusCode(ResultMap.STATUS_CODE_SYSTEM_ERROE);
			resultMap.setMessage(e.getMessage());
		}
		return resultMap;
	}
}
